#include "AnimalEnemy.h"

AnimalEnemy::AnimalEnemy(void){
	this->InitSprite(9);
	this->SetSprite(0, "images/Enemy/Animal/ApeIdle.tga");
	this->SetSprite(1, "images/Enemy/Animal/ApeAttack1.tga");
	this->SetSprite(2, "images/Enemy/Animal/ApeAttack2.tga");
	this->SetSprite(3, "images/Enemy/Animal/ApeAttack3.tga");
	this->SetSprite(4, "images/Enemy/Animal/ApeAttack4.tga");
	this->SetSprite(5, "images/Enemy/Animal/ApeWalking1.tga");
	this->SetSprite(6, "images/Enemy/Animal/ApeWalking2.tga");
	this->SetSprite(7, "images/Enemy/Animal/ApeWalking3.tga");
	this->SetSprite(8, "images/Enemy/Animal/ApeWalking4.tga");
	
	this->Init(Vector3D(), 60, 45);
	this->SetLife(2.5f);
	this->SetAttackDamage(0.5f);

	weapon.InitSprite(3);
	weapon.SetSprite(0, "images/Enemy/Animal/ApeWeapon1.tga");
	weapon.SetSprite(1, "images/Enemy/Animal/ApeWeapon2.tga");
	weapon.SetSprite(2, "images/Enemy/Animal/ApeWeapon3.tga");
	weapon.Init(this->GetPosition(), 20, this->GetHeight());

	spaceCheck.InitBox(this->GetPosition(), 2.0, this->GetHeight() / 2, 1.0);

	this->SetSpeed(100.0f);
	this->SetLengthOfSight(LINE_OF_SIGHT);

	attackStart = false;
	attackTime = false;
	idleTime = false;

	curState = IDLE;
	newState = IDLE;
}

AnimalEnemy::~AnimalEnemy(void){
}

void AnimalEnemy::Idle(void){
	if(this->GetOnGround()){
		if(!idleTime){
			this->SetSpriteImageNumber(0);

			for(vector<Object *>::iterator itr = TargetInfoList.begin(); itr != TargetInfoList.end(); ++itr){
				if(!(*itr)->GetBoundingBox(Object::FULLBODY).HitTest(this->GetBoundingBox(Object::FULLBODY))){
					rndNum = rand() % 100 + 1;
					if(rndNum < 25){
						this->SetDirection(Vector3D());
					}else{
						rndNum = rand() % 100 + 1;

						if(rndNum < 50)
							this->SetDirection(Vector3D(1.0, this->GetDirection().y, 0.0));
						else
							this->SetDirection(Vector3D(-1.0, this->GetDirection().y, 0.0));
					}
				}else{
					this->SetDirection(Vector3D());
				}
			}

			countdownTimer.StartTimer(0, 0, 5, 0);
			idleTime = true;
		}

		if(idleTime && countdownTimer.CheckIfTimerEnd()){
			//countdownTimer.StartTimer(0, 0, 5, 0);
			idleTime = false;
		}
	}
}

void AnimalEnemy::Attack(void){
	if(!attackStart){
		for(vector<Object *>::iterator itr = TargetInfoList.begin(); itr != TargetInfoList.end(); ++itr){
			//delete (*itr);

			if(this->weapon.GetBoundingBox(Object::FULLBODY).HitTest((*itr)->GetBoundingBox(Object::FULLBODY))){
				this->SetDirection(Vector3D());

				this->SetSpriteImageNumber(0);
				countdownTimer.StartTimer(0, 0, 0, 500);

				weapon.invertImage = this->invertImage;

				attackStart = true;
			}
		}
	}

	if(attackStart && countdownTimer.CheckIfTimerEnd()){
		this->PlaySpriteAnimation(1, 4, 500);

		if(this->GetSpriteImageNumber() == 2){
			attackTime = true;
			renderWeapon = true;
		}

		if(this->animationFinished){
			attackStart = false;
			this->SetSpriteImageNumber(0);
			newState = IDLE;
			attackTime = false;
			renderWeapon = false;
		}else if(attackTime){
			weapon.PlaySpriteAnimation(0, 2, 500);
			//weapon.SetSpriteImageNumber(1);
		}
	}
}

bool AnimalEnemy::UpdateObject(Vector3D offset, float elapsed){
	Enemy::UpdateObject(offset, elapsed);

	if(attackTime){
		for(vector<Object *>::iterator itr = TargetInfoList.begin(); itr != TargetInfoList.end(); ++itr){
			if(weapon.GetBoundingBox(Object::FULLBODY).HitTest((*itr)->GetBoundingBox(Object::FULLBODY))){
				(*itr)->ReduceLife(this->GetAttackDamage());
			}
		}

		weapon.UpdateObject(offset, elapsed);
	}

	if(enemyDetected || attackStart){
		newState = ATTACK;
	}else{
		newState = IDLE;
	}

	if(curState != newState){
		countdownTimer.StopTimer();
		curState = newState;
	}

	switch(curState){
		case IDLE:
			Idle();
			break;
		case ATTACK:
			Attack();
			break;
	}

	return false;
}